package com.sunlg.smarthome.contract.presenter;

import com.sunlg.smarthome.base.App;
import com.sunlg.smarthome.base.C;
import com.sunlg.smarthome.data.remote.MySubscriber;
import com.sunlg.smarthome.data.remote.Result;
import com.sunlg.smarthome.data.remote.SHServiceWrapper;
import com.sunlg.smarthome.contract.RegisterContract;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by sunlg on 2017/3/2.
 */

public class RegisterPresenter
        implements RegisterContract.Presenter {

    private RegisterContract.View mView;
    private SHServiceWrapper shServiceWrapper;

    public RegisterPresenter() {
        shServiceWrapper = App.getInstance().getServiceWrapper();
    }

    @Override
    public void register(String username, String password) {
        Map<String, Object> map = new HashMap<>();
        map.put("username", username);
        map.put("password", password);

        mView.showLoading();
        shServiceWrapper.register(map, new MySubscriber<Result>() {
                    @Override
                    public void onSuccess(Result data) {
                        mView.hideLoading();
                        if(data.getCode() == C.ActionStatus.SUCCESS){
                            mView.forward();
                        }else {
                            mView.showError(data.getMsg());
                        }
                    }

                    @Override
                    public void _onError(Throwable e) {
                        mView.showError(e.getMessage());
                    }
                });
    }

    @Override
    public void attachView(RegisterContract.View view) {
        mView = view;
    }

    @Override
    public void dettachView() {
        mView = null;
    }
}
